from paho.mqtt.client import Client, MQTTMessage
# from .setting import mqtthost
from serial import Serial

class mqttContoller(Client):
    def __init__(self, host, topic):
        self.host = host
        self.topic = topic
        Client.__init__(self)
        self.on_message = self.receivedmessagemethod

    def connecttohost(self):
        self.connect(self.host)
        self.subscribe(topic=self.topic, qos=2)

    def receivedmessagemethod(self, client, userdata, message):
        # TODO finish the method here
        print('topic: ' + message.topic)
        print('payload: ' + message.payload.decode('utf8'))


class deviceController(Serial):
    def __init__(self, com):
        self.com = com
        Serial.__init__(port=self.com)
        self.open()


if __name__ == '__main__':
    client = mqttContoller(host='dashiandjie.top', topic='server')
    print('started')
    client.connecttohost()
    while True:
        client.loop()
